package virtorina.server.domain;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import virtorina.client.room.dto.ComplaintDTO;
import virtorina.server.PMF;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Complaint implements Serializable {
	private static final long serialVersionUID = 1L;
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long ID;
	@Persistent
	private Integer type;
	/*
	 * 0 - скучный 1 - неоднозначный 2 - грубый 3 - неправильный 4 - нехватает
	 * тематик
	 */
	@Persistent
	private LinkedHashSet<Long> userID;
	@Persistent
	private Long questionID;

	public Complaint(Integer type, Long questionID) {
		super();
		this.type = type;
		this.userID = new LinkedHashSet<Long>();
		this.questionID = questionID;
	}

	public void setUserID(Long e) {
		this.userID.add(e);
	}

	public Long getID() {
		return ID;
	}

	public void setID(Long iD) {
		ID = iD;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public LinkedHashSet<Long> getUserID() {
		return userID;
	}

	public void setUserID(LinkedHashSet<Long> userID) {
		this.userID.addAll(userID);
	}

	public Long getQuestionID() {
		return questionID;
	}

	public void setQuestionID(Long questionID) {
		this.questionID = questionID;
	}

	public static LinkedList<ComplaintDTO> getTopDTOComplaints() {
		LinkedList<ComplaintDTO> rslt = new LinkedList<ComplaintDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery("select from " + Question.class.getName()
				+ " where NumOfComplaint > 0");
		q.setOrdering("NumOfComplaint DESC");
		Query complGetter = pm.newQuery("select from "
				+ Complaint.class.getName() + " where questionID == qID");
		complGetter.declareParameters("Long qID");
		q.setRange(0, 20);
		List<Question> questions = (List<Question>) q.execute();
		LinkedList<String> labels;
		ComplaintDTO cdto;
		for (Question quest : questions) {
			labels = Label.getLabelsNames(quest.getLabelsID());
			List<Complaint> cmpl = (List<Complaint>) complGetter.execute(quest
					.getID());
			cdto = new ComplaintDTO(quest.getQuestion(),
					new LinkedList<String>(quest.getAnswer()), labels);
			for (Complaint c : cmpl) {
				cdto.addComplaint(c.getID(), c.getType(), c.getUserID().size());
			}
			rslt.add(cdto);
		}
		pm.close();
		return rslt;
	}

	public static void saveALL(LinkedList<Complaint> list) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		for (Complaint c : list) {
			System.out.println(c.getQuestionID() + " " + c.getType() + " "
					+ c.getUserID().size());
			if (!c.getUserID().isEmpty()) {
				Query q = pm.newQuery("select from "
						+ Complaint.class.getName()
						+ " where type == tp && questionID == qp");
				q.declareParameters("Integer tp, Long qp");
				List<Complaint> rslt = (List<Complaint>) q.execute(c.getType(),
						c.getQuestionID());
				if (rslt.isEmpty()) {

					pm.makePersistent(c);
				} else {
					Complaint t = rslt.get(0);
					t.setUserID(c.getUserID());
					pm.makePersistent(t);
				}
			}
		}
		pm.close();
	}
}
